//
//  LSYWelcomeController.swift
//  LSYWeibo
//
//  Created by 刘诗岩 on 16/4/9.
//  Copyright © 2016年 刘诗岩. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class LSYWelcomeController: UIViewController {

    override func loadView() {
        
        view = UIImageView(image: UIImage(named: "ad_background"))
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // 加载控件
        setupUI()
        
        makeContrains()
    }

    private func setupUI(){
        
        view.addSubview(nameLabel)
        view.addSubview(headerIcon)
        
        headerIcon.layer.cornerRadius = 45
        headerIcon.sd_setImageWithURL(LSYUserAccountViewModel.sharedUserAccountViewModel.headImageURL)
        
        
    }
    
    override func viewDidAppear(animated: Bool) {
        
        
        
        startAnimation()
        
    }
    
    // MARK: - 设置控件约束
    private func makeContrains(){
        
        headerIcon.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(headerIcon.snp_centerX)
            make.top.equalTo(headerIcon.snp_top).offset(16)
        }
    }
    
    // MARK: - 入场动画
    private func startAnimation(){
        
        let offset = -UIScreen.mainScreen().bounds.height + 180.0
        
        nameLabel.alpha = 0
        
        headerIcon.snp_updateConstraints { (make) -> Void in
            
            make.bottom.equalTo(view.snp_bottom).offset(offset)
        }
        
        /**
                弹簧动画设置
        
        - parameter <Tduration:             动画时长
        - parameter delay:                  延迟
        - parameter usingSpringWithDamping: 阻尼系数
        - parameter initialSpringVelocity:  加速度
        - parameter options:                数组
        - parameter animations:             动画
        - parameter completion:             完成调用
        */
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    
                    self.nameLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName(LSYSwitchRootViewController, object: "Main")
                })
        }
    }
    
    
    // MARK: - 懒加载
    private lazy var headerIcon = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLabel: UILabel = UILabel(text: "欢迎回来", textColor: UIColor.darkGrayColor(), fontSize: 15)
//    private lazy var nameLabel: UILabel = {
//        
//        let nameLabel = UILabel()
//        
//        nameLabel.textColor = UIColor.darkGrayColor()
//        nameLabel.font = UIFont.systemFontOfSize(15)
//        nameLabel.text = "欢迎回来"
//        nameLabel.sizeToFit()
//        
//        return nameLabel
//        
//    }()

}
